package cn.itcast.hotel.config;

import cn.itcast.hotel.constants.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    /**
     * 初始化交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(MqConstants.HOTEL_EXCHANGE);
    }

    /**
     * 初始化新增队列
     *
     * @return
     */
    @Bean
    public Queue insertQueue() {
        return new Queue(MqConstants.HOTEL_INSERT_QUEUE);
    }

    /**
     * 初始化删除队列
     *
     * @return
     */
    @Bean
    public Queue deleteQueue() {
        return new Queue(MqConstants.HOTEL_DELETE_QUEUE);
    }

    /**
     * 绑定交换机和新增队列
     *
     * @param insertQueue
     * @param directExchange
     * @return
     */
    @Bean
    public Binding bindInsertQueue(Queue insertQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(insertQueue).to(directExchange).with(MqConstants.HOTEL_INSERT_KEY);
    }

    /**
     * 绑定交换机和删除队列
     *
     * @param deleteQueue
     * @param directExchange
     * @return
     */
    @Bean
    public Binding bindDeleteQueue(Queue deleteQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(deleteQueue).to(directExchange).with(MqConstants.HOTEL_DELETE_KEY);
    }
}
